package com.fqgj.sentry.controller.mc;

import com.fqgj.sentry.biz.mc.ApplicationBusiness;
import com.fqgj.sentry.biz.mc.vo.application.MerchantVo;
import com.fqgj.sentry.common.cache.RequestLocalInfo;
import com.fqgj.sentry.common.response.ApiOutput;
import com.fqgj.sentry.common.utils.ApiPage;
import com.fqgj.sentry.controller.mc.request.ApplicationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;



/**
 * Created by liupengpeng on 2017/7/14.
 */
@RestController
@RequestMapping("/application")
public class ApplicationController {

    @Autowired
    private ApplicationBusiness applicationBusiness;


    /**
     * 应用集合查询
     *
     * @param apiPage
     * @return
     */
    @PreAuthorize("hasPermission('','mc_application_read')")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiOutput list(@RequestBody ApiPage apiPage) {
        ApiPage apiPage1 = applicationBusiness.selectListForPage(RequestLocalInfo.getCurrentMerchantId(), apiPage);
        return new ApiOutput<>(apiPage1);
    }

    /**
     * 获取商户信息
     *
     * @return
     */
    @PreAuthorize("hasPermission('','mc_application_read')")
    @RequestMapping(value = "/merchant/info", method = RequestMethod.POST)
    public ApiOutput Login() {
        MerchantVo merchantInfo = applicationBusiness.selectByMerchantId(RequestLocalInfo.getCurrentMerchantId());
        return new ApiOutput<>("merchantInfo", merchantInfo);
    }


    /**
     * 应用删除
     *
     * @param applicationId
     * @return
     */
    @PreAuthorize("hasPermission('','mc_application_write')")
    @RequestMapping(value = "/{applicationId}/delete", method = RequestMethod.POST)
    public ApiOutput delete(@PathVariable(value = "applicationId") Long applicationId) {
        applicationBusiness.deleteById(applicationId);
        return new ApiOutput<>();
    }

    /**
     * 应用新增
     *
     * @param applicationVo
     * @return
     */
    @PreAuthorize("hasPermission('','mc_application_write')")
    @RequestMapping(value = "/inset", method = RequestMethod.POST)
    public ApiOutput save(@RequestBody ApplicationVo applicationVo) {
        applicationVo.setMerchantId(RequestLocalInfo.getCurrentMerchantId());
        applicationBusiness.insert(applicationVo);
        return new ApiOutput<>();
    }

    /**
     * 应用修改
     *
     * @param applicationVo
     * @return
     */
    @PreAuthorize("hasPermission('','mc_application_write')")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiOutput update(@RequestBody ApplicationVo applicationVo) {
        applicationBusiness.update(applicationVo);
        return new ApiOutput<>();
    }

}
